package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Template;
import com.itheima.service.TemplateService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.InnocuousThread;

import java.util.List;
import java.util.Map;

/**
 * @program: qingcheng
 * @description: 规格参数管理
 * @author: Mr.Gu
 * @create: 2021-07-17 11:27
 **/

@RestController
@RequestMapping("/template")
public class TemplateController {

    @Reference
    private TemplateService templateService;


    /**
     * 分页查询
     * @param //queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public Result findPage(Integer currentPage,Integer size,@RequestBody Map map){
        //
        try {
            String queryString = (String) map.get("name");

            QueryPageBean queryPageBean = new QueryPageBean(currentPage,size,queryString);

            PageResult pageResult = templateService.pageQuery(queryPageBean);

            return new Result(true, MessageConstant.QUERY_TEMPLATE_SUCCESS,pageResult);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_TEMPLATE_FAIL);
        }
    }


    /**
     * 会显数据
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        Template template = null;
        try{
            template= templateService.findById(id);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_TEMPLATE_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_TEMPLATE_SUCCESS,template);



    }

    /**
     * 新增模板
     * @param template
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Template template){
        try {
            templateService.add(template);
        } catch (RuntimeException e){
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_TEMPLATE_FAIL);
        }
        return new Result(true,MessageConstant.ADD_TEMPLATE_SUCCESS);
    }


    /**
     * 删除模板
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            templateService.delete(id);
        } catch (RuntimeException e){
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_TEMPLATE_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_TEMPLATE_SUCCESS);
    }


    /**
     * 更新模板
     * @param template
     * @return
     */
    @RequestMapping("/update")
    public Result update(@RequestBody Template template){
        try{
            templateService.update(template);
            return new Result(true,MessageConstant.EDIT_TEMPLATE_SUCCESS);
        } catch (RuntimeException e){
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_TEMPLATE_FAIL);
        }
    }


}
